package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    @Resource
    private SetmealMapper setmealMapper;

    @Transactional  //事务
    @Override
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //保存菜品
        dishMapper.insertDish(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!Objects.isNull(flavors) && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //保存口味
            dishFlavorMapper.insertDishFlavor(flavors);
        }
    }

    /**
     * 菜品的分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        Page<DishVO> page = dishMapper.selectDish(dish);

        return PageResult.builder().total(page.getTotal()).records(page.getResult()).build();
    }

    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        //判断起售
        if (dishMapper.statusCount(StatusConstant.ENABLE, ids) > 0) {
            log.info("在售菜品有 : {} 个", dishMapper.statusCount(StatusConstant.ENABLE, ids));
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //判断关联套餐
        if (dishMapper.setmealCount(ids) > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //都通过判断,开始删除
        //删除菜品关联口味
        dishFlavorMapper.deleteDishFlavor(ids);
        //删除菜品
        dishMapper.deleteDish(ids);
    }

    /**
     * 根据Id查询菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public DishVO selectDishById(Long id) {
        DishVO dish = dishMapper.selectDishById(id);
        dish.setFlavors(dishFlavorMapper.selectDishFlavorById(id));
        return dish;
    }

    /**
     * 修改菜品
     *
     * @param dishVO
     */
    @Override
    @Transactional
    public void updateDish(DishVO dishVO) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishVO, dish);//左边存右边

        if(dishVO.getFlavors().size() != 0){
            List<Long> list = new ArrayList<>();
            list.add(dish.getId());
            List<DishFlavor> flavors = dishVO.getFlavors();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            if(dishFlavorMapper.selectDishFlavorById(dish.getId()).size() != 0){
                //删除原口味
                dishFlavorMapper.deleteDishFlavor(list);
            }

            //添加现口味
            dishFlavorMapper.insertDishFlavor(flavors);
        }


        //修改菜品
        dishMapper.updateDish(dish);
    }

    /**
     * 修改(起售/停售)状态
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long id) {
        dishMapper.updateDish(Dish.builder().status(status).id(id).build());
        //如果当前要停售,并且有与当前菜品关联的套餐时
        if (status.equals(StatusConstant.DISABLE) && dishMapper.selectSetmealDishCount(id) > 0) {
            List<Long> setmealId = dishMapper.selectSetMealList(id);
            setmealId.forEach(aLong -> setmealMapper.updateSetMeal(Setmeal.builder().id(aLong)
                    .status(StatusConstant.DISABLE).build()));

        }
    }

    /**
     * 根据分类Id查询该分类下的菜品信息
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectCategory(Long categoryId) {

        return dishMapper.selectCategory(categoryId);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        //List<Dish> dishList =

        List<DishVO> dishVOList = dishMapper.selectDish(dish);

        for (DishVO d :dishVOList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectDishFlavorById(d.getId());
            d.setFlavors(flavors);
        }

        return dishVOList;
    }
}
